package groovy

import com.aliyun.dingtalkworkflow_1_0.models.QueryIntegratedTodoTaskResponseBody

import java.lang.reflect.Array
@Grab(group='com.microsoft.sqlserver', module='mssql-jdbc', version='9.2.1.jre11') // 如果你使用Groovy的Grab机制

import java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.Statement

static def queryDatabase(dbUrl,user,password,sql,hasRes=true,query){
    Connection connection = DriverManager.getConnection(dbUrl, user, password)
    Statement statement = connection.createStatement()
    ResultSet resultSet=null
    if (hasRes){  resultSet = statement.executeQuery(sql)
        query(resultSet)
    }
    else statement.execute(sql).toString()
    if (resultSet!=null) resultSet.close()
    statement.close()
    connection.close()
}
static def queryMysql(String sql,hasRes=true,query){
    def dbUrl = 'jdbc:mysql://192.168.1.14:3306;databaseName=opsa'
    def user = 'zzj'
    def password = '123'
    queryDatabase(dbUrl,user,password,sql,hasRes,query)
}
static def querySqlserver(String sql,hasRes=true,query){
    def dbUrl = 'jdbc:sqlserver://192.168.1.18:1433;databaseName=opsa;encrypt=true;trustServerCertificate=true;'
    def user = 'adm'
    def password = '123'
    queryDatabase(dbUrl,user,password,sql,hasRes,query)
}

static List<String> getTaskByPersonFromPlm(String name="陈总"){
    String sql = "select FlowNodeID_TX from fn_GetFlowInfo(\'${name}\',1) where 计划开始时间 > \'2025-01-20 17:27:10.010\'"
    def plmTask=[]
    querySqlserver(sql){resultSet->{
        while (resultSet.next()) {
            plmTask.add(resultSet.getString('FlowNodeID_TX')
            )
        }
    }
    }
    plmTask
}
static def synPlmTask(String taskId){
//    querySqlserver("exec SP_SendHttpMsg 'SendHttpSetting','{\"TreeID\":\"32791\",\"strALLID\":\"${taskId}\",\"LinkClassCode\":\"\",\"LinkID\":\"\",\"RelationType\":\"\",\"RelationTypeID\":\"\",\"SRelationDir\":\"\"}'",false){}
    querySqlserver("if exists(select 1 from TB_InterfaceFlow where FlowNodeID_TX='${taskId}')\n" +
            "    exec SP_SendHttpMsg 'SendHttpSetting','{\"TreeID\":\"32791\",\"strALLID\":\"${taskId}\",\"LinkClassCode\":\"\",\"LinkID\":\"\",\"RelationType\":\"\",\"RelationTypeID\":\"\",\"SRelationDir\":\"\"}'\n" +
            "else\n" +
            "    exec SP_SendHttpMsg 'SendHttpSetting','{\"TreeID\":\"32932\",\"strALLID\":\"${taskId}\",\"LinkClassCode\":\"\",\"LinkID\":\"\",\"RelationType\":\"\",\"RelationTypeID\":\"\",\"SRelationDir\":\"\"}'",false){}
}
static def synAllTasks(){
    def plmTaskList=getTaskByPersonFromPlm()
//    println("plmTaskList")
//    println(plmTaskList.size())
//    plmTaskList.forEach {println(it)}
    List<QueryIntegratedTodoTaskResponseBody.QueryIntegratedTodoTaskResponseBodyResultList> dingTaskInformationList=QueryDingTalkTask.getAllDingUserTaskList()
    def dingTaskList=[]
    dingTaskInformationList.forEach {dingTaskList.add(it.activityId)}
//    println("dingTaskList")
//    println(dingTaskList.size())
//    dingTaskList.forEach {println(it)}

    def plmMoreTaskSet = plmTaskList.toSet() - dingTaskList.toSet()
    List<QueryIntegratedTodoTaskResponseBody.QueryIntegratedTodoTaskResponseBodyResultList> dingTaskMoreInformationList=[]
    dingTaskInformationList.forEach {
        if (!plmTaskList.contains(it.activityId))
            dingTaskMoreInformationList.add(it)
    }

//    def dingTalkMoreTaskSet = dingTaskList.toSet() - plmTaskList.toSet()
    println("plmMoreTaskSet")
    println(plmMoreTaskSet.size())
    def wrongTaskList=[]
    plmMoreTaskSet.forEach {
        println(it)
        if (!wrongTaskList.contains(it)){
            synPlmTask(it)
            sleep(5000)
        }
    }
////plmMoreTaskSet.forEach {synPlmTask(it)}
//    println("dingTalkMoreTaskSet")
//    def token=GetDingTokenScript.getTokenFromRedis()
    println("cancelDingTasks")
    dingTaskMoreInformationList.forEach {
        println(it.activityId)}
//        def processInstanceIdList=dingTaskMoreInformationList.collect{it.processInstanceId}
//        processInstanceIdList.forEach {
//            def activityIds = dingTaskMoreInformationList
//                    .findAll {info-> info.processInstanceId == it }
//                    .collect { it.activityId }
//            println(CancelTask.cancelTask(token,it, activityIds))
//        }

//    sleep(5000)

//def token=GetDingTokenScript.getTokenFromRedis()
//dingTalkMoreTaskSet.forEach {
//    println(CancelTask.cancelTask(token,HttpRequest.getParentById1(it), it))
//    }
}

static  void main(String[] args){

    synAllTasks()
//    synPlmTask("T0000254169")
//    synPlmTask("T0000252887")
//    def token=GetDingTokenScript.getTokenFromRedis()
//    println(token)
//    println(CancelTask.cancelTask(token,"e4tVmIGoReW9DkeoDBx2_Q09661737768643", "T0000252748"))
//    println(CancelTask.cancelTask(token,"HnWwxqUKRfypM4Z_bjqqfA09661738720711","T0000254025"))

}

